package com.hongxun.project.assets.bModelItem.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxun.project.assets.bModelItem.mapper.BModelItemMapper;
import com.hongxun.project.assets.bModelItem.domain.BModelItem;
import com.hongxun.project.assets.bModelItem.service.IBModelItemService;
import com.hongxun.common.utils.text.Convert;

/**
 * 模块子 服务层实现
 *
 * @author hongxun
 * @date 2019-07-10
 */
@Service
public class BModelItemServiceImpl implements IBModelItemService {
    @Autowired
    private BModelItemMapper bModelItemMapper;

    /**
     * 查询模块子信息
     *
     * @param id 模块子ID
     * @return 模块子信息
     */
    @Override
    public BModelItem selectBModelItemById(String id) {
        return bModelItemMapper.selectBModelItemById(id);
    }

    /**
     * 查询模块子列表
     *
     * @param bModelItem 模块子信息
     * @return 模块子集合
     */
    @Override
    public List<BModelItem> selectBModelItemList(BModelItem bModelItem) {
        return bModelItemMapper.selectBModelItemList(bModelItem);
    }

    /**
     * 新增模块子
     *
     * @param bModelItem 模块子信息
     * @return 结果
     */
    @Override
    public int insertBModelItem(BModelItem bModelItem) {
        bModelItem.setId(UUID.randomUUID().toString());
        return bModelItemMapper.insertBModelItem(bModelItem);
    }

    /**
     * 修改模块子
     *
     * @param bModelItem 模块子信息
     * @return 结果
     */
    @Override
    public int updateBModelItem(BModelItem bModelItem) {
        return bModelItemMapper.updateBModelItem(bModelItem);
    }

    /**
     * 删除模块子对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBModelItemByIds(String ids) {
        return bModelItemMapper.deleteBModelItemByIds(Convert.toStrArray(ids));
    }

    @Override
    public void insertOrReplaceOrDelete(List<BModelItem> entities) {
        for (BModelItem b : entities) {
            if (b.getDatastate().equals("删除")) {
                bModelItemMapper.deleteBModelItemById(b.getId());
            } else {
                BModelItem model = bModelItemMapper.selectBModelItemById(b.getId());
                if (model != null) {
                    bModelItemMapper.updateBModelItem(b);
                } else {
                    bModelItemMapper.insertBModelItem(b);
                }
            }
        }
    }

}
